package com.yunhoo.zh.bean;

import com.yunhoo.zh.db.impl.UserAccountImpl;
import com.yunhoo.zh.db.impl.UserImpl;
import com.yunhoo.zh.model.User;
import com.yunhoo.zh.model.UserAccount;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LoginZhBean implements Serializable {

    private User user;

    public LoginZhBean() {
        user = new User();
    }

    /* 登录 */
    public String login() {

        Long flag = UserImpl.login(user);

        if (flag == -1) {
            //查询失败
            System.out.println(" * * * " + "查询失败" + "  * * * ");
        } else if (flag == -2) {
            //用户名不存在
            System.out.println(" * * * " + "用户名不存在" + "  * * * ");
        } else if (flag == -3) {
            //密码错误
            System.out.println(" * * * " + "密码错误" + "  * * * ");
        } else if (flag > 0) {
            //登录成功
            FacesContext context = FacesContext.getCurrentInstance();
            Map<String,String> paramMap = context.getExternalContext().getRequestParameterMap();
            paramMap.put("id_u",flag.toString());
            return initAccount(flag);
        } else {
            //未知异常
            System.out.println(" * * * " + "未知异常" + "  * * * ");
        }
        return "login_zh";
    }

    private String initAccount(Long id_u) {
        List<UserAccount> actli = UserAccountImpl.queryListByUserId(id_u);
        String path = "/page/zh/";
        if (actli == null || actli.isEmpty()) return path + "initAccount";
        return path + "accountList";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
